package com.marvels.loanapply.channel.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.loanapply.channel.dto.ChannelUserListParamDTO;
import com.marvels.loanapply.channel.dto.ChannelUserListResultDTO;
import com.marvels.loanapply.channel.dto.SysUserDTO;
import com.marvels.loanapply.channel.dto.UserChannelInfoParamDTO;
import com.marvels.loanapply.channel.dto.UserChannelInfoResultDTO;
import com.marvels.loanapply.channel.dto.UserChannelRelation;
import com.marvels.loanapply.channel.dto.UserChannelRelationDTO;
import com.marvels.loanapply.channel.service.UserChannelInfoService;

/**
 * 入驻渠道管理
 * @author 吴俊峰
 */
@RestController
@RequestMapping("loanApply/channel/service")
public class UserChannelInfoController {

	@Autowired
	private UserChannelInfoService userChannelInfoService;
	
	/**
	 * 获取用户渠道列表
	 * @param userChannelInfoParamDTO
	 * @return
	 */
	@RequestMapping("/userChannelList")
	@ResponseBody
	public MarvelsResultDTO queryUserChannelList(@RequestBody UserChannelInfoParamDTO userChannelInfoParamDTO){
		try {
			List<UserChannelInfoResultDTO> list = userChannelInfoService.queryUserChannelList(userChannelInfoParamDTO);
			MarvelsLogUtil.getInstance().info("获取用户渠道列表返回结果=======》》》" + list);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("获取用户渠道列表异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("获取用户列表异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**
	 * 导出用户渠道列表
	 * @param userChannelInfoParamDTO
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadUserChannelExcel")
	public void downloadUserChannelExcel(UserChannelInfoParamDTO userChannelInfoParamDTO,HttpServletRequest request, HttpServletResponse response){
		OutputStream os = null;
		HSSFWorkbook workbook = null;
		try {
			workbook = userChannelInfoService.downloadUserChannelExcel(userChannelInfoParamDTO, request, response);
	        os = response.getOutputStream();// 取得输出流        
	        response.setHeader("Content-disposition", "attachment; filename="+ new String("入驻平台列表.xls".getBytes("UTF-8"),"ISO8859-1"));  
	        response.setContentType("application/x-download");// 定义输出类型      
	        workbook.write(os);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("导出excel异常！", e);
		} catch (IOException e) {
			MarvelsLogUtil.getInstance().error("导出excel异常！", e);
		}finally{
			 if (os != null) {
                 try { 
                	 os.close(); 
                }  catch (IOException e) { 
	                 MarvelsLogUtil.getInstance().error("关闭流异常！",e);
                } 
             } 
		}
	}
	
	
	/**
	 * 插入用户渠道
	 * @param userChannelInfoParamDTO
	 * @return
	 */
	@RequestMapping("/insertUserChannelInfo")
	@ResponseBody
	public MarvelsResultDTO insertUserChannelInfo(@RequestBody UserChannelInfoParamDTO userChannelInfoParamDTO){
		try {
			userChannelInfoService.insertUserChannelInfo(userChannelInfoParamDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("插入用户渠道异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("插入用户渠道异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	
	/**
	 * 修改渠道信息
	 * @param userChannelInfoParamDTO
	 * @return
	 */
	@RequestMapping("/operateChannelStatus")
	@ResponseBody
	public MarvelsResultDTO operateChannelStatus(@RequestBody UserChannelInfoParamDTO userChannelInfoParamDTO){
		try {
			UserChannelInfoResultDTO result = userChannelInfoService.operateChannelStatus(userChannelInfoParamDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), result);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("修改渠道状态异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("修改渠道状态异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	
	/**
	 * 获取渠道用户信息列表
	 * @param userChannelInfoParamDTO
	 * @return
	 */
	@RequestMapping("/queryChannelRegisterUserList")
	@ResponseBody
	public MarvelsResultDTO queryChannelRegisterUserList(@RequestBody ChannelUserListParamDTO channelUserListParamDTO){
		try {
			List<ChannelUserListResultDTO> list = userChannelInfoService.queryChannelRegisterUserList(channelUserListParamDTO);
			MarvelsLogUtil.getInstance().info("获取渠道用户信息列表返回结果=======》》》" + JSON.toJSONString(list));
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("获取渠道用户信息列表异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("获取渠道用户信息列表异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**
	 * 导出渠道用户信息列表
	 * @param userChannelInfoParamDTO
	 * @param request
	 * @param response
	 */
	@RequestMapping("downloadChannelRegisterUserExcel")
	public void downloadChannelRegisterUserExcel(ChannelUserListParamDTO channelUserListParamDTO,HttpServletRequest request, HttpServletResponse response){
		OutputStream os = null;
		HSSFWorkbook workbook = null;
		try {
			workbook = userChannelInfoService.downloadChannelRegisterUserExcel(channelUserListParamDTO, request, response);
	        os = response.getOutputStream();// 取得输出流        
	        response.setHeader("Content-disposition", "attachment; filename="+ new String("平台用户列表.xls".getBytes("UTF-8"),"ISO8859-1"));  
	        response.setContentType("application/x-download");// 定义输出类型      
	        workbook.write(os);
		}catch(MarvelsBusinessException e){
			MarvelsLogUtil.getInstance().error("导出excel异常！", e);
		} catch (IOException e) {
			MarvelsLogUtil.getInstance().error("导出excel异常！", e);
		}finally{
			 if (os != null) {
                 try { 
                	 os.close(); 
                }  catch (IOException e) { 
	                 MarvelsLogUtil.getInstance().error("关闭流异常！",e);
                } 
             } 
		}
	}
	
	
	/**
	 * 获取系统用户列表
	 * @return
	 */
	@RequestMapping("/selectSysUserList")
	@ResponseBody
	public MarvelsResultDTO selectSysUserList(){
		try {
			/*List<SysUserDTO> userList = new ArrayList<SysUserDTO>();
			SysUserDTO dto = new SysUserDTO();
			dto.setRealName("系统管理员");
			dto.setUserName("system");
			dto.setUserCode("system");
			SysUserDTO dto3 = new SysUserDTO();
			dto3.setRealName("杨宁波");
			dto3.setUserName("老d");
			dto3.setUserCode("dongd");
			SysUserDTO dto4 = new SysUserDTO();
			dto4.setRealName("董婷");
			dto4.setUserName("testDT01");
			dto4.setUserCode("testDT01");
			userList.add(dto);
			userList.add(dto3);
			userList.add(dto4);*/
			List<SysUserDTO> userList = userChannelInfoService.selectSysUserList();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), userList);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("获取系统用户列表异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**
	 * 保存用户渠道关联信息
	 * @return
	 */
	@RequestMapping("/saveUserChannelRelation")
	@ResponseBody
	public MarvelsResultDTO saveUserChannelRelation(@RequestBody UserChannelRelationDTO dto){
		try {
			userChannelInfoService.saveUserChannelRelation(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("保存用户渠道关联信息异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**
	 * 查询用户渠道关联信息
	 * @return
	 */
	@RequestMapping("/selectUserChannelRelation")
	@ResponseBody
	public MarvelsResultDTO selectUserChannelRelation(@RequestBody UserChannelRelationDTO dto){
		try {
			Map<String, Object> map = userChannelInfoService.selectUserChannelRelation(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), map);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error("查询用户渠道关联信息异常=======》》》", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
}
